__author__ = 'Me'

import ogre.renderer.OGRE as ogre
from math import pi,sin,cos


def create2x2Quad(sc,name,mat):
    '''
    create2x2Quad: returns a ManualObject in the given scenemanager's scene with the given name and material.
    The returned manual object is a 2x2 quad centered at the origin
    '''

    m = sc.createManualObject(name)

    m.clear()

    m.begin(mat,ogre.RenderOperation.OT_LINE_STRIP)

    m.position(-1,-1,0)
    m.position(1,-1,0)
    m.position(1,1,0)
    m.position(-1,1,0)

    m.index(0)
    m.index(1)
    m.index(2)
    m.index(3)
    m.index(0)

    m.end()

    box = ogre.AxisAlignedBox()
    box.setInfinite()
    m.setBoundingBox(box)

    return m

def createUserQuad(sc,name,mat,verts):
    '''
    createUserQuad: returns a ManualObject in the given scenemanager's scene with the given name and material.
    The returned manual object is a quad with vertices give by the the first four entries in verts,
    which should be a list of tuples that can be unpacked a valid vertices. This method does nothing at all to validate
    the given vertices to make sure they're coplanar.
    '''

    m = sc.createManualObject(name)

    m.begin(mat,ogre.RenderOperation.OT_LINE_STRIP)

    m.position(*verts[0])
    m.position(*verts[1])
    m.position(*verts[2])
    m.position(*verts[3])

    m.index(0)
    m.index(1)
    m.index(2)
    m.index(3)
    m.index(0)

    m.end()

    return m


def createLine3d(sc,name,mat,verts):
    '''
    createLine3d: returns a ManualObject in the given scenemanager's scene with the given name and material.
    The object is a line that connects the vertices specified in verts in order. Verts should be a list of
    tuples unpackable as valid vertices.
    '''
    m = sc.createManualObject(name)

    m.begin(mat,ogre.RenderOperation.OT_LINE_STRIP)

    for v in verts:
        m.position(*v)

    for i in range(len(verts)):
        m.index(i)

    m.end()

    return m

def createCircle3d(sc,name,mat,r,acc):
    '''
    Documentation Here.
    '''
    m = sc.createManualObject(name)

    m.begin(mat,ogre.RenderOperation.OT_LINE_STRIP)

    theta = 2*pi/acc
    point_index = 0

    for i in range(acc):
        m.position(r*cos(i*theta),0,r*sin(i*theta))
        m.index(point_index)
        point_index += 1

    m.index(0)

    m.end()

    sc.getRootSceneNode().createChildSceneNode().attachObject(m)

    return m



